/************

 Facebook manager
 refs:
 	- http://developers.facebook.com/docs/reference/plugins
	- http://developers.facebook.com/docs/reference/dialogs
	- http://fbdevwiki.com

 *************/

social.facebook = new ( function ()
{
	var self = this;
	self._appid = null;
	self._status = null;
	self._access_token = null;
	self.cbacks = {
		'init': null,
		'login': null,
		'feed': null,
		'post': null,
		'share': null,
		'publish': null
	};

	self.init = function ()
	{
		var root = document.createElement ( 'div' );
		root.id = 'fb-root';
		document.body.appendChild ( root );

		if ( typeof FB == 'undefined' ) return;

		FB.init (
		{
			appId: self._appid,
			status: true,
			cookie: true,
			oauth: true
		} );

		if ( ! self._appid ) return;

		FB.getLoginStatus ( self._login_status_cback );
	};

	self._set_session = function ( s )
	{
		self._status = s.status;
		if ( s.authResponse )
		{
			self._access_token = s.authResponse.accessToken;
			self._uid = s.authResponse.userID;
		}
	};

	self._login_status_cback = function ( res )
	{
		console.debug ( 'fb login status cback: %o', res );
		self._set_session ( res );
		if ( self.is_logged () )
		{
			self._load_me ( self.cbacks [ 'init' ] );
		}
		else
		{
			if ( self.cbacks [ 'init' ] ) self.cbacks [ 'init' ] ();
		}

	};

	self._login_cback = function ( res )
	{
		console.debug ( 'fb login cback: %o', res );
		if ( res.session )
		{
			self._set_session ( res );
			self._load_me ( self.cbacks [ 'login' ] );
		}
	};

	self._load_me = function ( cback )
	{
		FB.api ( '/me', function ( me )
		{
			console.debug ( 'fb load me: %o', me );
			self._me = me;
			if ( cback ) cback ();
		} );
	};

	self.login = function ()
	{
		if ( self.is_logged () )
		{
			self._load_me ( self.cbacks [ 'login' ] );
		}
		else
		{
			var scope = 'email';
			FB.login ( self._login_cback, { scope: scope } );
		}
	};

	self.user_login = function ( cback )
	{
		if ( self.is_logged () )
		{
			var params = {
				action : 'social.ajax.facebook_login',
				access_token : self._access_token
			};
			social.ajax ( params, function ( res )
			{
				if ( cback ) cback ( res );
			} );
		}
	};

	self.get_login = function ()
	{
		return String.formatDict ( social.templates [ 'facebook-login' ], { text: 'Login tramite Facebook' } );
	};

	self.is_logged = function ()
	{
		return ( self._status == 'connected' );
	};

	self.get_user = function ()
	{
		return self._me;
	};

	self.check_login = function ( cback )
	{
		if ( self.is_logged () )
			cback ();
		else
		{
			self.cbacks [ 'login' ] = cback;
			self.login ();
		}
	};

	self.check_perm = function ( perm, cback )
	{
		FB.api ( '/me/permissions', function ( res )
		{
			console.debug ( 'fb check perm: %o', res );
			if ( ! res.error )
			{
				self._perms = res.data [ 0 ];
				if ( self._perms [ perm ] )
				{
					cback ();
					return;
				}
			}
			self.ask_perm ( perm, cback );
		} );
	};

	self.ask_perm = function ( perm, cback )
	{
		FB.ui ( 
		{
			method: 'permissions.request',
			display: 'popup', // HACK: to avoid conflict with object_enh
			perms: perm
		}, cback );
	};

	self._parse = function ( dest_div ) 
	{
		if ( typeof FB == 'undefined' ) return;
		if ( dest_div ) dest_div = $ ( dest_div );

		FB.XFBML.parse ( dest_div );
	};

	self._format = function ( type, data )
	{
		var fields = [];
		data.iterate ( function ( value, field )
		{
			if ( value )
				fields.push ( String.formatDict ( social.templates [ 'facebook-data' ], { field: field, value: value } ) );
		} );
		return String.formatDict ( social.templates [ 'facebook-div' ], { type: type, fields: fields.join ( ' ' ) } );
	};

	self.get_like = function ( data )
	{
		return self._format ( 'like', data );
	};

	self.like = function ( dest_div, data )
	{
		$ ( dest_div, self.get_like ( data ) );
	};

	self.get_send = function ( data )
	{
		return self._format ( 'send', data );
	};

	self.send = function ( dest_div, data )
	{
		$ ( dest_div, self.get_send ( data ) );
	};

	self.get_comments = function ( data )
	{
		return self._format ( 'comments', data );
	};

	self.comments = function ( dest_div, data )
	{
		$ ( dest_div, self.get_comments ( data ) );
	};

	self.feed = function ( link, name, caption, description )
	{
		FB.ui (
		{
			method: 'feed',
			display: 'popup', // HACK: to avoid conflict with object_enh
			link: link,
			name: name,
			caption: caption,
			description: description
		}, self.cbacks [ 'feed' ] );
	};

	self.share = function ( url )
	{
		FB.ui ( 
		{
			method: 'stream.share',
			display: 'popup', // HACK: to avoid conflict with object_enh
			u: url
		}, self.cbacks [ 'share' ] );
	};

	self.publish = function ( message, name, description )
	{
		FB.ui ( 
		{
			method: 'stream.publish',
			display: 'popup', // HACK: to avoid conflict with object_enh
			message: message,
			attachment:
			{
				name: name,
				description: description
			}
		}, self.cbacks [ 'publish' ] );
	};

	self.post = function ( message, destination )
	{
		if ( ! destination ) destination = 'me';
		self.check_perm ( 'publish_stream', function ()
		{
			self._post ( message, destination );
		} );

	};

	self._post = function ( message, destination )
	{
		FB.api ( '/' + destination + '/feed', 'post', { message: message }, function ( res )
		{
			console.debug ( res );
			if ( res.error )
			{
				console.error ( res.error.message );
			}
			else if ( self.cbacks [ 'post' ] )
				self.cbacks [ 'post' ] ( res );
		} );
	};

	self.post_list = function ( page_id, cback )
	{
		if ( ! page_id ) page_id = social.facebook._appid;
		self.check_login ( function ()
		{
			FB.api ( '/' + page_id + '/posts', function ( res )
			{
				cback ( res.data );
			} );
		} );
	};
} ) ();
